package com.crm.system.service.impl.datasearch;

import com.beust.jcommander.internal.Lists;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.DataSearchEnum;
import com.crm.model.qo.datasearch.DataSearchQO;
import com.crm.model.vo.datasearch.DataSearchResultVO;
import com.crm.system.service.contractManager.CustomerContractService;
import com.crm.system.service.customerManager.CustomerService;
import com.crm.system.service.datasearch.DataSearchService;
import com.crm.system.service.opportunityManager.OpportunityService;
import com.crm.system.service.orderManager.CustomerProductOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 数据搜索
 * @Author lixq-f
 * @Date 2021/5/12
 * @Version V1.0
 **/
@Service
public class DataSearchServiceImpl implements DataSearchService {


    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerContractService contractService;
    @Resource
    private CustomerProductOrderService orderService;
    @Resource
    private OpportunityService opportunityService;

    /**
     * @Description: 数据搜索
     * @Param: [searchType, searchName]
     * @Return: java.util.List<com.crm.model.vo.datasearch.DataSearchResultVO>
     * @Author: lixq-f
     * @Date: 2021/5/12
     **/
    @Override
    public DhPageInfo<DataSearchResultVO> searchDataByCondition(DataSearchQO searchQO) {

        PageHelper.startPage(searchQO.getPageIndex(), searchQO.getPageSize());
        List<DataSearchResultVO> resultList = null;

        if (searchQO.getSearchType() == DataSearchEnum.CUSTOMER_NAME.getType()) {
            resultList = customerService.selectDataByCustomerName(searchQO.getSearchName());
        }

        if (searchQO.getSearchType() == DataSearchEnum.CUSTOMER_CONTRACT.getType()) {
            resultList = contractService.selectDataByContractNum(searchQO.getSearchName());
        }

        if (searchQO.getSearchType() == DataSearchEnum.CUSTOMER_ORDER.getType()) {
            resultList = orderService.selectDataByOrderNum(searchQO.getSearchName());
        }

        if (searchQO.getSearchType() == DataSearchEnum.CUSTOMER_OPPORTUNITY.getType()) {
            resultList = opportunityService.selectDataByOpportunityName(searchQO.getSearchName());
        }

        if (CollectionUtils.isEmpty(resultList)) {
            resultList = Lists.newArrayList();
        }

        PageInfo<DataSearchResultVO> pageInfo = new PageInfo<>(resultList);
        return new DhPageInfo<>(pageInfo, resultList);
    }


}
